package com.github.captcha.spring.configure;

import com.github.captcha.generator.manager.ImageCaptchaGenerator;
import com.github.captcha.generator.manager.ImageTransform;
import com.github.captcha.generator.manager.impl.generator.CacheImageCaptchaGenerator;
import com.github.captcha.generator.manager.impl.transform.Base64ImageTransform;
import com.github.captcha.resource.manager.ImageCaptchaResourceManager;
import com.github.captcha.resource.manager.ResourceStore;
import com.github.captcha.resource.manager.impl.DefaultImageCaptchaResourceManager;
import com.github.captcha.resource.manager.impl.DefaultResourceStore;
import com.github.captcha.spring.plugins.SpringMultiImageCaptchaGenerator;
import com.github.captcha.spring.properties.ImageCaptchaProperties;
import com.github.captcha.spring.properties.SliderCaptchaCacheProperties;
import com.github.captcha.validator.manager.ImageCaptchaValidator;
import com.github.captcha.validator.manager.validator.BasicCaptchaTrackValidator;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.core.annotation.Order;

@Order
@Configuration
@AutoConfigureAfter(CacheStoreAutoConfiguration.class)
@EnableConfigurationProperties({ImageCaptchaProperties.class})
public class ImageCaptchaAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ResourceStore resourceStore() {
        return new DefaultResourceStore();
    }

    @Bean
    @ConditionalOnMissingBean
    public ImageCaptchaResourceManager imageCaptchaResourceManager(ResourceStore resourceStore) {
        return new DefaultImageCaptchaResourceManager(resourceStore);
    }

    @Bean
    @ConditionalOnMissingBean
    public ImageTransform imageTransform() {
        return new Base64ImageTransform();
    }

    @Bean
    @ConditionalOnMissingBean
    public ImageCaptchaGenerator imageCaptchaGenerator(ImageCaptchaProperties prop,
                                                       ImageCaptchaResourceManager captchaResourceManager,
                                                       ImageTransform imageTransform,
                                                       BeanFactory beanFactory) {
        // 构建多验证码生成器
        ImageCaptchaGenerator imageCaptchaGenerator = new SpringMultiImageCaptchaGenerator(captchaResourceManager, imageTransform, beanFactory);

        SliderCaptchaCacheProperties cache = prop.getCache();
        if (cache != null && Boolean.TRUE.equals(cache.getEnabled()) && cache.getCacheSize() > 0) {
            // 增加缓存处理
            imageCaptchaGenerator = new CacheImageCaptchaGenerator(imageCaptchaGenerator, cache.getCacheSize(), cache.getWaitTime(), cache.getPeriod());
        }
        // 初始化
        imageCaptchaGenerator.init(prop.getInitDefaultResource());

        return imageCaptchaGenerator;
    }

    @Bean
    @ConditionalOnMissingBean
    public ImageCaptchaValidator imageCaptchaValidator() {
        return new BasicCaptchaTrackValidator();
    }

}
